import Axios from 'axios'
import qs from 'qs'
let url = "";
if (process.env.NODE_ENV === 'production') {
    url = "http://ttapi.research.itcast.cn/";
} else {
    url = "http://ttapi.research.itcast.cn/";
}
Axios.defaults.withCredentials = false;
let timer;
const axios = Axios.create({
        baseURL: url, //查一下自己的地址
        timeout: 10000,
        responseType: 'json',
        withCredentials: false,
        headers: {
            'Content-type': 'application/x-www-form-urlencoded;charset=UTF-8'
        }
    })
    //请求拦截，统一加loading
axios.interceptors.request.use(config => {
    // 清除计时器
    clearTimeout(timer)
        //   ? config.data = qs.stringify({...config.data})
    config.method === 'post' ?
        config.data = qs.stringify({...config.data }) :
        config.params = {...config.params };
    config.headers['Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8';
    return config
}, error => {
    return Promise.reject(error.data.error.message)
})
export default axios;